<?php
class Administrator_playlist_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	/*Playlist Management*/
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');	
		$is_hilight = $this->input->post('is_hilight');	
		$is_recommend = $this->input->post('is_recommend');	
		$playlist_alias = $this->input->post('playlist_alias');	
		$playlist_name = $this->input->post('playlist_name');	
		$playlist_brief = $this->input->post('playlist_brief');	
		$media_id = $this->input->post('media_id');	
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');

		//Insert playlist
		$data = array(
			'is_publish' => $is_publish,
			'is_hilight' => $is_hilight,
			'is_recommend' => $is_recommend,
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('playlist',$data);
		$playlist_id = $this->db->insert_id();
		
		//Insert playlist Info
		$data = array(
			'playlist_id' => $playlist_id,
			'playlist_alias' => $playlist_alias,
			'playlist_name' => $playlist_name,
			'playlist_brief' => $playlist_brief,
			'playlist_song' => ($media_id ? implode(':',$media_id):NULL)
		);
		$this->db->insert('playlist_info',$data);	

		//Insert image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $playlist_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'playlist/images/',
				'dimension' => array(				
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage = array(
				'playlist_file_name' => $data->file_name,
				'playlist_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('playlist_info',$dataImage,array('playlist_id' => $playlist_id));
	}
	
	function content_management_edit($playlist_id)
	{
		$is_publish = $this->input->post('is_publish');
		$is_hilight = $this->input->post('is_hilight');
		$is_recommend = $this->input->post('is_recommend');	
		$playlist_alias = $this->input->post('playlist_alias');	
		$playlist_name = $this->input->post('playlist_name');	
		$playlist_brief = $this->input->post('playlist_brief');	
		$media_id = $this->input->post('media_id');	
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');

		//Insert playlist
		$data = array(
			'is_publish' => $is_publish,
			'is_hilight' => $is_hilight,
			'is_recommend' => $is_recommend,
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('playlist',$data,array('playlist_id' => $playlist_id));
		
		//Insert playlist Info
		$data = array(
			'playlist_alias' => $playlist_alias,
			'playlist_name' => $playlist_name,
			'playlist_brief' => $playlist_brief,
			'playlist_song' => ($media_id ? implode(':',$media_id):NULL)
		);
		$this->db->update('playlist_info',$data,array('playlist_id' => $playlist_id));	

		//Insert image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $playlist_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'playlist/images/',
				'dimension' => array(					
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));			

			//delete temp
			@unlink($this->temp_folder.$value);
			$dataInfo = $this->db->get_where('playlist_info',array('playlist_id' => $playlist_id),1)->row();
			$map = directory_map($this->upload_folder.'playlist/images/', 1);
			foreach($map as $folder):
				@unlink($this->upload_folder.'playlist/images/'.$folder.'/'.$dataInfo->playlist_file_name.'.'.$dataInfo->playlist_file_ext);
			endforeach;
			
			$dataImage = array(
				'playlist_file_name' => $data->file_name,
				'playlist_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->update('playlist_info',$dataImage,array('playlist_id' => $playlist_id));
	}	
	
	function ajax_delete_image($playlist_id)
	{
		$dataImage = $this->db->get_where('playlist_info',array('playlist_id' => $playlist_id),1)->row();
		$this->db->update('playlist_info',array('playlist_file_name' => '','playlist_file_ext' => ''),array('playlist_id' => $playlist_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'playlist/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'playlist/images/'.$folder.'/'.$dataImage->playlist_file_name.'.'.$dataImage->playlist_file_ext);
		endforeach;
		exit;
	}
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'playlist/images/', 1);
		foreach($id as $value):
			$dataImage = $this->db->get_where('playlist_info',array('playlist_id' => $value))->result();
			$tables = array('playlist','playlist_info');
			$this->db->where('playlist_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				@unlink($this->upload_folder.'playlist/images/'.$folder.'/'.$dataImage->playlist_file_name.'.'.$dataImage->playlist_file_ext);
			endforeach;					
		endforeach;;		
	}	
}

?>